﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShow.BLL
{
    public class StatisticsCategoryService: BaseService<StatisticsCategory>
    {
        //查询出所有父级类型商品
        public List<StatisticsCategory> GetStatisticsCategories()
        {
            List<StatisticsCategory> statisticsCategories = new List<StatisticsCategory>();
            using (var db = new CrookedShowEntities())
            {
                var result = from C in db.Category
                             where C.ParentId == 0
                             select new StatisticsCategory() {
                                 Category_Id = C.Id,
                                 Category_Name=C.Name
                             };
                return result.ToList();
            }
        }
        /// <summary>
        /// 订单商品
        /// </summary>
        /// <returns></returns>
        public List<StatisticsCategory> GetStatisticsCategoriess()
        {
            List<StatisticsCategory> statisticsCategories = new List<StatisticsCategory>();
            using (var db = new CrookedShowEntities())
            {
                var resultstatistics = from c in db.Category
                                       join a in db.Product_Spu
                                       on c.Id.ToString() equals a.One_Category_Id.ToString()
                                       join b in db.Order_Detail
                                       on a.Id.ToString() equals b.Product_Id.ToString()
                                       where c.ParentId == 0
                                       group new {c} by new { c.Id, c.Name}
                                        into e
                                       select new StatisticsCategory()
                                       {
                                           Category_Id = e.Key.Id,
                                           Category_Name = e.Key.Name,
                                           Category_Count=e.Count()
                                       };
                return resultstatistics.ToList();
            }
        }
    }
}

